<?php
$title="Product List";
$css="productList.css";
include_once 'common/AdminTop.php';
include_once 'authentic.php';
include_once '../Class/DB.php';
$db=new DB();
$numPerPage=8;
$totalProduct=$db->get_all_product();
$totalPage=ceil(count($totalProduct)/$numPerPage);
$page=$_GET['page']; 
if (!isset($page)) {
 $page=1;
} 
if($page>$totalPage)
{
	$page--;
}
if($page<=0)
{
	$page=1;
}
$startCount=($page-1)*$numPerPage;
$products=$db->get_sort_product($startCount,$numPerPage);
?>
<div id="userlist" align="center">
<table id="userlistTable" border="1" style="margin-top:20px;font-size: 12pt;">
	<tbody>
		<tr style="color:white;height: 25px;">
			<th width="150px"><p>Name</p></th>
			<th width="72px"><p>Type</p></th>
			<th width="95px"><p>Origin Price</p></th>
			<th width="98px"><p>Current Price</p></th>
			<th width="60px"><p>Stock</p></th>
			<th width=""><p>OnTime</p></th>
			<th width="30px"></th>
		</tr>
		<?php 
		$i=0;
		while(isset($products[$i]))
		{
			$theType=$db->get_tid_types($products[$i]["type"]);
			if(!isset($theType[0]))
			$theType[0]["name"]="Undefined";
		?>
		<tr style="color:white;">
			<td width="" align="center">
			<a href="modifyproduct.php?page=<?php echo $page;?>&id=<?php echo $products[$i]["pid"];?>"><?php echo $products[$i]["p_name"];?></a><div class="show_product">
			<img alt="good" style="width:50px;height:55px;" src="<?php if(file_exists("../Image/product_img/".$products[$i]["pid"].".jpg")){echo "../Image/product_img/".$products[$i]["pid"].".jpg";}
										else {echo "../Image/product_img/item_none.png";}?>">
			</div>
			</td>
			<td align="center"><p><?php echo $theType[0]["name"];?></p></td>
			<td align="center"><p>$<?php echo $products[$i]["ori_price"];?></p></td>
			<td align="center"><p>$<?php echo $products[$i]["act_price"];?></p></td>
			<td align="center" ><p><?php echo $products[$i]["count"];?></p></td>
			<td width="" ><p><?php echo $products[$i]["onTime"];?></p></td>
		    <td width="" align="center"><p><a href="productDelete.php?id=<?php echo $products[$i]["pid"];?>&page=<?php echo $page;?>">
				<img alt="x" src="../image/delete.png" width="14">
			</a></p></td>
		</tr>
		<?php 
		$i++;
		}
		?>
	</tbody>
</table>
        	<div id="page">
			<?php if ($page != 1) { ?>
			<a href="productList.php?page=<?php echo $page - 1;?>">Back</a> 
			<?php }
			for ($i=1;$i<=$totalPage;$i++) {  
				if($i==$page){
					?>
					<p><?php echo $i;?></p>
			<?php continue;}
			?>
			<a href="productList.php?page=<?php echo $i;?>"><?php echo $i ;?></a>
			<?php
			}
			if ($page<$totalPage) {
			?>
				<a href="productList.php?page=<?php echo $page + 1;?>">Next</a>
			<?php
					} 
			?>
			
			</div>
</div>
<?php 
include_once 'common/AdminTail.php';
?>